//import org.springframework.security.providers.UsernamePasswordAuthenticationToken as AuthToken
//import org.springframework.security.context.SecurityContextHolder as SCH

class ContactUsController {

    def authenticateService
    def daoAuthenticationProvider
    def emailerService

    static Map allowedMethods = [save: 'POST', update: 'POST']

    def index = {
        def contactUs = new ContactUs()

        // TODO: 
        // If user is logged in we will populate what we can.
//        if (authenticateService.isLoggedIn()) {
//        }

        return [contactUs: contactUs]
    }

    def send = {
        def contactUs = new ContactUs()
        contactUs.properties = params

        if (contactUs.validate()) {
            if (params.captcha.toUpperCase() != session.captcha) {
                contactUs.errors.rejectValue('captcha', 'captcha.match.error')
                render view: 'index', model: [contactUs: contactUs]
                return
            }
            println sendMail {
                to "${grailsApplication.config.contactUs.to.address}"
                from "${contactUs.email}"
                subject "${contactUs.subject}"
                body "${contactUs.message}"
            }
        } else {
            render view: 'index', model: [contactUs: contactUs]
        }
    }
}
